<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\HrefButton;
use App\Models\JxtArticle;
use App\Models\JxtCategory;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章管理';

    protected function title()
    {
        $category_id = request('category_id', 0);
        if (!empty($category_id)) {
            $category = JxtCategory::find($category_id);
            return $category->title;
        }
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new JxtArticle());
        $category_id = request('category_id', 0);
        if (!empty($category_id)) {
            $grid->disableCreateButton();
            $grid->model()->where('category_id', $category_id);
            $grid->tools(function ($tools) use ($category_id) {
                $tools->append(new HrefButton('fa-plus', '新增', 'btn-success', 'articles/create?category_id=' . $category_id));
            });
        }

        $grid->column('id', __('Id'));
        $grid->column('title', __('标题'));
        $grid->column('image', __('图片'))->display(function ($value) {
            if (!empty($value)) {
                return "<img width='100' src='" . config('filesystems.disks.admin.url') . '/' . $value . "'/>";
            }
        });
        $grid->column('category.title', __('分类'));
        $grid->column('status', __('是否开放'))->display(function ($value) {
            $class = $value == JxtArticle::STATUS_SHOW ? 'label-success' : 'label-danger';
            return "<span class='label " . $class . "'>" . JxtArticle::$status[$value] . "</span>";
        });
        $grid->column('created_at', __('创建日期'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(JxtArticle::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('category.title', __('所属分类'));
        $show->field('title', __('标题'));
        $show->field('image', __('图片'))->setEscape(false)->as(function ($value) {
            return "<img width='100' src='" . config('filesystems.disks.admin.url') . '/' . $value . "'/>";
        });
        $show->field('content', __('内容'))->unescape();
        $show->field('status', __('是否显示'))->as(function ($value) {
            return JxtArticle::$status[$value];
        });
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new JxtArticle());
        $category_id = 0;
        $id = request()->route()->parameters()['article'] ?? 0;
        $category_id = request('category_id');
        if ($form->isEditing() && !empty($id)) {
            $category_id = JxtArticle::find($id)->category->id;
        }
        if (empty($category_id)) {
            $form->select('category_id', '所属分类')->options(JxtCategory::selectOptions(null, ''))->rules('required', [
                'required' => '请选择所属分类！'
            ]);
        } else {
            $form->hidden('category_id')->default(request('category_id'));
        }

        $form->text('title', __('标题'))->rules('required', [
            'required' => '请输入标题！'
        ]);
        $form->text('introduce', __('描述'))->help('一小段文字性介绍');
        //关于我们联系我们不需要图片
        if($id == 3 || $id == 4){
            $form->image('image', __('图片'))->uniqueName()->move('article/images/' . date('Ymd'));
        }else{
            $form->image('image', __('图片'))->uniqueName()->move('article/images/' . date('Ymd'))->required();
        }
        $form->editor('content', __('内容'))->rules('required', [
            'required' => '请输入内容！'
        ]);
        $is_open = [
            'off' => ['value' => 0, 'text' => JxtArticle::$status[0], 'color' => 'danger'],
            'on' => ['value' => 1, 'text' => JxtArticle::$status[1], 'color' => 'success'],
        ];
        $form->switch('status', '属性')->states($is_open)->default(JxtArticle::STATUS_SHOW);
        if ($category_id == 5 || $category_id == 6) {
            $form->saved(function (Form $form) use ($id) {
                // 跳转页面
                admin_toastr('更新成功', 'success');
                return redirect('/admin/articles/' . $id . '/edit');
            });
        }

        return $form;
    }
}
